home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Returning a reference
- Date: 10 Jan 1996 17:04:55 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan10120455@g7240065.bridge.bst.bls.com>
- References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: 100754.2730@compuserve.com's message of Wed, 10 Jan 1996 08:11:19
- GMT
-
- In article <4cvsm2$5ig@dub-news-svc-4.compuserve.com> 100754.2730@compuserve.com (Martin Aupperle) writes:
-
- : Borland C++ V4.5 does not allow to return a reference to a local
- : variable:
-
- : int &doIt() {
-
- : int i = 7;
- : return i; // syntax error
- : }
-
- : I remember that I once had a compiler that did allow it (it gave me
- : only a warning).
- : Which one is right? I think that it should be an error because after
- : the function has terminated, the reference has no data object it is
- : bound to any more.
-
- Many compilers will allow this, though you are correct it will refer to
- data which has been cleaned off the stack.
- Many compilers will also allow:
-
- int*
- doIt(void)
- {
- int i = 7;
- return &i;
- }
-
- Which suffers from the same problem.
- Both compilers are right. It is not required by the standard that the
- compiler issue diagnostic messages for this construct but it is not
- non-conforming (double-negatives are groovy) for issuing such messages.
-
- Regards
-
- -A.
- --
- | A.Champion |
-